package com.greenDao.db;

import android.app.Activity;

import com.greenDao.pojo.DaoMaster;
import com.greenDao.pojo.DaoMaster.DevOpenHelper;
import com.greenDao.pojo.DaoSession;
import com.greenDao.pojo.EventDao;
import com.greenDao.pojo.MovieDao;

public class GreenDaoHelper {

	private static final String DATABASE_NAME = "greenDaoAndroid.db";

	private static DevOpenHelper devOpenHelper;
	private static DaoSession daoSession;

	private static MovieDao movieDao = null;
	private static EventDao eventDao = null;

	private static GreenDaoHelper instance = null;

	public static GreenDaoHelper getInstance(Activity activity) {
		if (instance == null) {
			synchronized (GreenDaoHelper.class) {
				GreenDaoHelper inst = instance;
				if (inst == null) {
					synchronized (GreenDaoHelper.class) {
						instance = new GreenDaoHelper(activity);
					}
				}
			}
		}
		return instance;
	}

	public static GreenDaoHelper getInstance() {
		return getInstance(null);
	}

	private GreenDaoHelper(Activity activity) {
		if (activity != null) {
			devOpenHelper = new DaoMaster.DevOpenHelper(activity, DATABASE_NAME, null);
			DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
			daoSession = daoMaster.newSession();
		}
	}

	public void onCreate() {
		DaoMaster.createAllTables(devOpenHelper.getWritableDatabase(), true);
	}

	public static MovieDao getMovieDao() {
		if (movieDao == null) {
			movieDao = daoSession.getMovieDao();
		}
		return movieDao;
	}

	public static EventDao getEventDao() {
		if (eventDao == null) {
			eventDao = daoSession.getEventDao();
		}
		return eventDao;
	}
}
